<?php

require_once ("./class/H.class.php");
require_once ("./class/database.class.php");
require_once ("./class/Snoopy.class.php");

$departUrl = 'http://01.actv.szcgroup.cn/activity/type/special/appoint/department?page=1&act_id=rlNGTw3dszMMeGwvRVrrNA%3D%3D&scid=sc7EB4D4C5BF402AC1';

$snoopy = new Snoopy();
$snoopy->rawheaders = [
    'X-Requested-With' => 'XMLHttpRequest'
];
$snoopy->fetch($departUrl);

$data = json_decode($snoopy->getResults(),1);
if($data['errcode'] == '0000'){
    foreach($data['data'] as $k => $v){
        $regionItem = $dbm->get("tb_region",['id','rid'],['name'=>str_replace("\r\n",'',$v['street'])]);
       // $regionItem = $dbm->get("tb_region",['id','rid'],['name'=>"沙井中心客运站"]);

        $dbm->insert('tb_address',
            [
                'address' => str_replace("\r\n",'',$v['address']),
                'contact'=> str_replace("\r\n",'',$v['contact']),
                'district' => str_replace("\r\n",'',$v['district']),
                'district_id' => $regionItem != null ? $regionItem['rid']:0,
                "lat" =>  $v['lat'],
                "lng" =>  $v['lng'],
                "opentime" =>  str_replace("\r\n",'',$v['open_time']),
                "sortby" =>  0,
                "street" => str_replace("\r\n",'', $v['street']),
                "street_id" =>  $regionItem != null ? $regionItem['id']:0,
            ]
        );
        echo ($k.'---'.$regionItem['id'].'-'.$regionItem['rid'].'-'.$v['street']."..success...".chr(10));
        sleep(1);

    }

}

//H::p($data,1);
/*
 *
$regionUrl = "http://01.actv.szcgroup.cn/activity/comm/region?id=2&level=2&act_id=rlNGTw3dszMMeGwvRVrrNA%3D%3D&scid=sc7EB4D4C5BF402AC1";

$snoopy = new Snoopy();
$snoopy->rawheaders = [
    'X-Requested-With' => 'XMLHttpRequest'
];
$snoopy->fetch($regionUrl);

$data = json_decode($snoopy->getResults(),1);

if($data['errcode'] == '0000'){
    foreach($data['region']['children'] as $k => $v){
        $dbm->insert('tb_region',['name' => $v['text'],'rid'=> 0,'ctime' => time()]);
        $cid = $dbm->id();
        echo ($cid."..success...".chr(10));
        foreach( $v['children'] as $c => $cv) {
            $dbm->insert('tb_region',['name' => $cv['text'],'rid'=> $cid,'ctime' => time()]);
            echo ($c."..success...".chr(10));
            sleep(1);
        }

    }

}
*/

